package com.east.controller.dictionary;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.east.bean.DictionaryItems;
import com.east.service.Dictionary.DictionaryService;
import com.east.utils.PageJson;
import com.east.utils.Pagenation;
import com.east.utils.Result;
import com.east.utils.ResultUtil;
import com.github.pagehelper.Page;
/**
 * 字典表设置
 * 所有基础设置接口全在这里面
 * @author Administrator
 *
 */
@RestController
@RequestMapping("dictionary")
public class DictionaryController {
	@Autowired
	DictionaryService dictionaryService;
	
	/**
	 * 合同类型
	 * 分页
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("contactType")
	@RequiresPermissions("dictionary:contactType")
	public Result ContactType(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
		(pagenation, DictionaryName.CONTRACT_TYPE_XIAOSHOU.getCode(), DictionaryName.CONTRACT_TYPE_XIAOSHOU.getName(), key);
		Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 合同类型
	 * 字典调用
	 * @param key
	 * @return
	 */
	@GetMapping("contactTypeDic")
	@RequiresPermissions("dictionary:contactTypeDic")
	public Result contactTypeDic( ){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
		( DictionaryName.CONTRACT_TYPE_XIAOSHOU.getCode(), DictionaryName.CONTRACT_TYPE_XIAOSHOU.getName(), null);
		return ResultUtil.success(page);
	}
	
	
	
	/**
	 * 重要程度
	 * 分页
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("importance")
	@RequiresPermissions("dictionary:importance")
	public Result supimportance(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
		(pagenation, DictionaryName.CUSTMER_IMPORTANCE.getCode(), DictionaryName.CUSTMER_IMPORTANCE.getName(), key);
		Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 重要程度
	 * 字典调用
	 * @param key
	 * @return
	 */
	@GetMapping("importanceDic")
	@RequiresPermissions("dictionary:importanceDic")
	public Result findByimportanceDic( ){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
		( DictionaryName.CUSTMER_IMPORTANCE.getCode(), DictionaryName.CUSTMER_IMPORTANCE.getName(), null);
		return ResultUtil.success(page);
	}
	
	/**
	 * 采购公司类型
	 * 分页
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("supCompanyType")
	@RequiresPermissions("dictionary:supCompanyType")
	public Result supCompanyType(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
		(pagenation, DictionaryName.COMPANY_PURCHASE_TYPE.getCode(), DictionaryName.COMPANY_PURCHASE_TYPE.getName(), key);
		Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 采购公司类型
	 * 字典调用
	 * @param key
	 * @return
	 */
	@GetMapping("supCompanyTypeDic")
	@RequiresPermissions("dictionary:supCompanyTypeDic")
	public Result findBysupCompanyTypeDic( ){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
		( DictionaryName.COMPANY_PURCHASE_TYPE.getCode(), DictionaryName.COMPANY_PURCHASE_TYPE.getName(), null);
		return ResultUtil.success(page);
	}
	
	/**
	 * 销售公司类型
	 * 分页
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("companyType")
	@RequiresPermissions("dictionary:companyType")
	public Result findByCompanyType(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
		(pagenation, DictionaryName.COMPANY_SALE_TYPE.getCode(), DictionaryName.COMPANY_SALE_TYPE.getName(), key);
		Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 销售公司类型
	 * 字典调用
	 * @param key
	 * @return
	 */
	@GetMapping("companyTypeDic")
	@RequiresPermissions("dictionary:companyTypeDic")
	public Result findByCompanyTypeDic( ){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
		( DictionaryName.COMPANY_SALE_TYPE.getCode(), DictionaryName.COMPANY_SALE_TYPE.getName(), null);
		return ResultUtil.success(page);
	}
	
	/**
	 * 员工在职状态
	 * 分页
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("service")
	@RequiresPermissions("dictionary:service")
	public Result findByService(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
		(pagenation, DictionaryName.STAFF_JOB_ANARCHY.getCode(), DictionaryName.STAFF_JOB_ANARCHY.getName(), key);
		Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 员工在职状态
	 * 字典调用
	 * @param key
	 * @return
	 */
	@GetMapping("serviceDic")
	@RequiresPermissions("dictionary:serviceDic")
	public Result findByServices( ){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
		( DictionaryName.STAFF_JOB_ANARCHY.getCode(), DictionaryName.STAFF_JOB_ANARCHY.getName(), null);
		return ResultUtil.success(page);
	}
	/**
	 * 员工专业
	 * 分页
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("professional")
	@RequiresPermissions("dictionary:professional")
	public Result findByProfessional(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
				(pagenation, DictionaryName.STAFF_PROFESSIONOL_TYPE.getCode(), DictionaryName.STAFF_PROFESSIONOL_TYPE.getName(), key);
				Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 员工专业
	 * 字典调用
	 * @param key
	 * @return
	 */
	@GetMapping("professionalDic")
	@RequiresPermissions("dictionary:professionalDic")
	public Result findByProfessionalDic(){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
				( DictionaryName.STAFF_PROFESSIONOL_TYPE.getCode(), DictionaryName.STAFF_PROFESSIONOL_TYPE.getName(), null);
		return ResultUtil.success(page);
	}
	/**
	 * 户口性质
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("character")
	@RequiresPermissions("dictionary:character")
	public Result findByCharacter(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
				(pagenation, DictionaryName.STAFF_HOUSEHOLD_REGISER.getCode(), DictionaryName.STAFF_HOUSEHOLD_REGISER.getName(), key);
				Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 户口性质
	 * 字典调用
	 * @param key
	 * @return
	 */
	@GetMapping("characterDic")
	@RequiresPermissions("dictionary:characterDic")
	public Result findByCharacter(){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
				( DictionaryName.STAFF_HOUSEHOLD_REGISER.getCode(), DictionaryName.STAFF_HOUSEHOLD_REGISER.getName(), null);
		return ResultUtil.success(page);
	}
	/**
	 * 岗位设置
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("post")
	@RequiresPermissions("dictionary:post")
	public Result findBypost(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
		(pagenation, DictionaryName.STAFF_POSITION_TYPE.getCode(), DictionaryName.STAFF_POSITION_TYPE.getName(), key);
		Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	
	/**
	 * 岗位设置
	 * 字典调用
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("postDic")
	@RequiresPermissions("dictionary:postDic")
	public Result findBypostDic(){
		List<DictionaryItems> listfindByDictionaryItem = dictionaryService.listfindByDictionaryItem
		( DictionaryName.STAFF_POSITION_TYPE.getCode(), DictionaryName.STAFF_POSITION_TYPE.getName(), null);
		return ResultUtil.success(listfindByDictionaryItem);
	}
	
	/**
	 * 项目类型
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("projectType")
	@RequiresPermissions("dictionary:projectType")
	public Result findByProjectType(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
		(pagenation, DictionaryName.PROJECT_TYPE.getCode(), DictionaryName.PROJECT_TYPE.getName(), key);
		Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 项目状态
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("projectState")
	@RequiresPermissions("dictionary:projectState")
	public Result findByProjectState(Pagenation pagenation, String key,DictionaryItems dictionaryItems){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
		(pagenation, dictionaryItems.getDiCode(),dictionaryItems.getDiName(), key);
		Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	
	/**
	 * 项目类型
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("projectTypeDic")
	@RequiresPermissions("dictionary:projectTypeDic")
	public Result findByProjectTypeDic(){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
		( DictionaryName.PROJECT_TYPE.getCode(), DictionaryName.PROJECT_TYPE.getName(), null);
		return ResultUtil.success(page);
	}
	/**
	 * 项目状态
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("projectStateDic")
	@RequiresPermissions("dictionary:projectStateDic")
	public Result findByProjectStateDic( DictionaryItems dictionaryItems){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
		( dictionaryItems.getDiCode(),dictionaryItems.getDiName(), null);
		return ResultUtil.success(page);
	}
	
	/**
	 * 销售负责人
	 * @param pagenation
	 * @param key
	 * @return
	 */
	@GetMapping("staffSale")
	@RequiresPermissions("dictionary:staffSale")
	public Result findBystaffSale(){
		 List<DictionaryItems> findBystaffSale = dictionaryService.findBystaffSale
		( Integer.parseInt(DictionaryName.Staff_Sale.getCode()));
		return ResultUtil.success(findBystaffSale);
	}
	/**
	 * 所有人员
	 * @return
	 */
	@GetMapping("staff")
	@RequiresPermissions("dictionary:staff")
	public Result findBystaff(){
		 List<DictionaryItems> findBystaffSale = dictionaryService.findBystaff();
		return ResultUtil.success(findBystaffSale);
	}
	/**
	 * 所有项目
	 * @return
	 */
	@GetMapping("project")
	@RequiresPermissions("dictionary:project")
	public Result findByProject(){
		 List<DictionaryItems> findBystaffSale = dictionaryService.findByProject();
		return ResultUtil.success(findBystaffSale);
	}
	/**
	 * 销售公司
	 * @return
	 */
	@GetMapping("saleCompany")
	@RequiresPermissions("dictionary:saleCompany")
	public Result findByCompany(){
		 List<DictionaryItems> findBystaffSale = dictionaryService.findBySaleCompany();
		return ResultUtil.success(findBystaffSale);
	}
		
	/**
	 * 科目
	 * @param key
	 * @return
	 */
	@GetMapping("subject")
	@RequiresPermissions("dictionary:subject")
	public Result findBySubject(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
				(pagenation, DictionaryName.FEE_SUBJECT.getCode(), DictionaryName.FEE_SUBJECT.getName(), key);
				Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 科目
	 * 字典调用、、
	 * @return
	 */
	@GetMapping("subjectDic")
	@RequiresPermissions("dictionary:subjectDic")
	public Result findBysubjectDic(){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
				( DictionaryName.FEE_SUBJECT.getCode(), DictionaryName.FEE_SUBJECT.getName(), null);
		return ResultUtil.success(page);
	}
	/**
	 * 付款类型
	 * @param key
	 * @return
	 */
	@GetMapping("payment")
	@RequiresPermissions("dictionary:payment")
	public Result findBypayment(Pagenation pagenation, String key){
		Page<DictionaryItems> page = dictionaryService.pagefindByDictionaryItem
				(pagenation, DictionaryName.CONTRACT_PAY_TYPE.getCode(), DictionaryName.CONTRACT_PAY_TYPE.getName(), key);
				Object pageJson = new PageJson<DictionaryItems>().pageJson(page);
		return ResultUtil.success(pageJson);
	}
	/**
	 * 付款类型
	 * 字典调用、、
	 * @return
	 */
	@GetMapping("paymentDic")
	@RequiresPermissions("dictionary:paymentDic")
	public Result findBypaymentDic(){
		List<DictionaryItems> page = dictionaryService.listfindByDictionaryItem
				( DictionaryName.CONTRACT_PAY_TYPE.getCode(), DictionaryName.CONTRACT_PAY_TYPE.getName(), null);
		return ResultUtil.success(page);
	}
	
	
	
	/**
	 * 字典表详情
	 * @param dictionaryItems
	 * @return
	 */
	@GetMapping("findOne")
	@RequiresPermissions("dictionary:findOne")
	public Result findByfindOne(DictionaryItems dictionaryItems){
		DictionaryItems findByOne = dictionaryService.findByOne(dictionaryItems);
		return ResultUtil.success(findByOne);
	}
	/**
	 * 字典表修改
	 * @param dictionaryItems
	 * @return
	 */
	@PostMapping("update")
	@RequiresPermissions("dictionary:update")
	public Result findByupdate(DictionaryItems dictionaryItems){
		int updeteDictionaryItems = dictionaryService.UpdeteDictionaryItems(dictionaryItems);
		if(updeteDictionaryItems>0) {
			return ResultUtil.success("修改成功");
		}else {
			return ResultUtil.success("修改失败");
		}
		
		
	}
	/**
	 *  字典表添加
	 * @param type
	 * @param dictionaryItems
	 * @param codeDic 项目状态code
	 * @param nameDic 项目状态name
	 * @return
	 */
	@PostMapping("save")
	@RequiresPermissions("dictionary:save")
	public Result findBySave(String type,DictionaryItems dictionaryItems,String codeDic,String nameDic){
		DictionaryName[] values = DictionaryName.values();
		String name=null;
		String code=null;
		boolean state=false;
		if(StringUtils.isNotBlank(codeDic)&&StringUtils.isNotBlank(nameDic)) {
			code=codeDic;
			name=nameDic;
		}else {
			for (DictionaryName dictionaryName : values) {
				if(type.equals(dictionaryName.getType())) {
					if(type.equals(DictionaryName.PROJECT_TYPE.getType())) {
						code=dictionaryName.getCode();
						name=dictionaryName.getName();
						state=true;
					}else {
						code=dictionaryName.getCode();
						name=dictionaryName.getName();
					}
					
					break;
				}
				
			}
		}
		
		int saveDictionaryItems = dictionaryService.saveDictionaryItems(code, name, dictionaryItems,state);
		if(saveDictionaryItems>0) {
			return ResultUtil.success("添加成功");
		}else {
			return ResultUtil.success("添加失败");
		}
	}
	/**
	 * 字典表删除
	 * @param dictionaryItems
	 * @return
	 */
	@DeleteMapping("del")
	@RequiresPermissions("dictionary:del")
	public Result findByDel(DictionaryItems dictionaryItems){
		int saveDictionaryItems = dictionaryService.delDictionaryItems(dictionaryItems);
		if(saveDictionaryItems>0) {
			return ResultUtil.success("删除成功");
		}else {
			return ResultUtil.success("删除失败");
		}
	}
}
